package com.wxsong.authplatform.authweb.controller;

import com.google.common.base.Joiner;
import com.google.common.collect.Maps;
import com.wxsong.authplatform.authweb.util.Base64Util;
import com.wxsong.authplatform.authweb.controller.common.Const;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Map;

public class BaseController {


    public int getSize(Integer rows) {
        return null == rows || rows==0 ? Const.pageSize : rows;
    }

    public int getFrom(Integer page, Integer rows) {
        return null == page || page==0 ? Const.pageFrom : (page-1)*rows;
    }

    public Map easyUIMap(Long total, List list){
        Map easyUIMap = Maps.newHashMap();
        easyUIMap.put("rows", list);
        easyUIMap.put("total", total);
        return easyUIMap;
    }


    public Long[] decodeIds(String idStr) {
        if(StringUtils.isBlank(idStr)){
            return new Long[]{};
        }
        String[] idArray = idStr.split(",");
        Long[] ids = new Long[idArray.length];
        for (int i=0; i<idArray.length; i++){
            ids[i] = Long.valueOf(Base64Util.decode(idArray[i]));
        }
        return ids;
    }
    public String decodeIdsToString(String idStr) {
        if(StringUtils.isBlank(idStr)){
            return "";
        }
        String[] idArray = idStr.split(",");
        String[] ids = new String[idArray.length];
        for (int i=0; i<idArray.length; i++){
            ids[i] = Base64Util.decode(idArray[i]);
        }
        return Joiner.on(",").join(ids);
    }

    public Long[] ids(String idStr) {
        if(StringUtils.isBlank(idStr)){
            return new Long[]{};
        }
        String[] idArray = idStr.split(",");
        Long[] ids = new Long[idArray.length];
        for (int i=0; i<idArray.length; i++){
            ids[i] = Long.valueOf(idArray[i]);
        }
        return ids;
    }
}
